package com.triones.yjj.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.triones.yjj.entity.ActivityDO;
import com.triones.yjj.entity.InviteRewardDO;
import com.triones.yjj.entity.OrderDO;
import com.triones.yjj.entity.bo.*;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.service.ActivityService;
import com.triones.yjj.service.MemberDistributionRecordService;
import com.triones.yjj.service.QrService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 活动管理
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/activity")
public class ActivityController extends BaseController {

    private final ActivityService activityService;

    private final MemberDistributionRecordService memberDistributionRecordService;

    private final QrService qrService;

    /**
     * 查询活动列表
     */
    @GetMapping("/list")
    public R<List<ActivityVO>> getList(ActivityListParams params) {
        return R.ok(activityService.getList(params));
    }

    /**
     * 获取活动详情
     */
    @GetMapping(value = "/{id}")
    public R<ActivityVO> getDetailById(@NotNull(message = "活动id不能为空") @PathVariable Long id) {
        return R.ok(activityService.getDetailById(id));
    }

    /**
     * 查询带访问权限的活动列表（分页）
     */
    @GetMapping("/visitAuth/pageList")
    public TableDataInfo<VisitAuthActivityListVO> visitAuthActivityPageList(ActivityListParams params, PageQuery pageQuery) {
        return activityService.visitAuthActivityPageList(params, pageQuery);
    }

    /**
     * 设置所有员工访问该活动
     */
    @PostMapping("/allStaffVisit/{id}")
    public R<Void> setAllStaffVisit(@PathVariable Long id) {
        return toAjax(activityService.setAllStaffVisit(id));
    }

    /**
     * 添加活动
     */
    @PostMapping
    public R<Void> saveActivity(@RequestBody ActivityDO dto) {
        return toAjax(activityService.saveActivity(dto));
    }

    /**
     * 编辑活动
     */
    @PutMapping
    public R<Void> editActivity(@RequestBody ActivityDO dto) {
        return toAjax(activityService.editActivity(dto));
    }

    /**
     * 查询活动列表（分页）
     */
    @GetMapping("/pageList")
    public TableDataInfo<ActivityListVO> pageList(ActivityListParams params, PageQuery pageQuery) {
        return activityService.pageList(params, pageQuery);
    }

    /**
     * 活动详情统计
     */
    @GetMapping(value = "/detailStats/{id}")
    public R<Map> activityDetailStats(@NotNull(message = "活动id不能为空") @PathVariable Long id) {
        return R.ok(activityService.activityDetailStats(id));
    }

    /**
     * 查询活动订单列表（分页）
     */
    @GetMapping("/orderInfo/pageList")
    public TableDataInfo<ActivityOrderListVO> activityOrderList(ActivityManageDetailParams params, PageQuery pageQuery) {
        return activityService.activityOrderList(params, pageQuery);
    }

    /**
     * 查询活动拼团列表（分页）
     */
    @GetMapping("/orderGroupPurchase/pageList")
    public TableDataInfo<ActivityOrderGroupPurchaseListVO> activityOrderGroupPurchaseList(ActivityManageDetailParams params, PageQuery pageQuery) {
        return activityService.activityOrderGroupPurchaseList(params, pageQuery);
    }

    /**
     * 查询活动退款列表（分页）
     */
    @GetMapping("/refund/pageList")
    public TableDataInfo<RefundListVO> refundPageList(RefundListParams params, PageQuery pageQuery) {
        return activityService.refundPageList(params, pageQuery);
    }

    /**
     * 查询活动邀请奖励列表（分页）
     */
    @GetMapping("/inviteReward/pageList")
    public TableDataInfo<InviteRewardListVO> inviteRewardPageList(InviteRewardListParams params, PageQuery pageQuery) {
        return activityService.inviteRewardPageList(params, pageQuery);
    }

    /**
     * 查询活动详情
     */
    @GetMapping("/detail/{id}")
    public R<ActivityVO> getInfoById(@NotNull(message = "活动id不能为空") @PathVariable Long id) {
        return R.ok(activityService.getInfoById(id));
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(activityService.remove(id));
    }

    /**
     * 邀请奖励核销
     */
    @PostMapping("/inviteReward/verification")
    public R<Void> inviteRewardVerification(@Validated @RequestBody InviteRewardDO dto) {
        if (dto.getVerificationCode() == null) {
            throw new SecurityException("核销码不能为空");
        }
        return toAjax(activityService.inviteRewardVerification(dto));
    }

    /**
     * 查询活动草稿列表（分页）
     */
    @GetMapping("/draft/pageList")
    public TableDataInfo<ActivityListVO> draftPageList(PageQuery pageQuery) {
        return activityService.draftPageList(pageQuery);
    }

    /**
     * 可移动团列表（分页）
     */
    @GetMapping("/canMoveTeam/pageList")
    public TableDataInfo<CanMoveTeamVO> canMoveTeamPageList(CanMoveTeamParams params, PageQuery pageQuery) {
        return activityService.canMoveTeamPageList(params,pageQuery);
    }

    /**
     * 活动分销统计
     */
    @GetMapping("/distributionStats/{activityId}")
    public R<Map> distributionStats(@NotNull(message = "活动ID不能为空") @PathVariable Long activityId) {
        return R.ok(memberDistributionRecordService.distributionStats(activityId));
    }

    /**
     * 查询分销佣金列表（分页）
     */
    @GetMapping("/distribution/pageList")
    public TableDataInfo<DistributionVO> distributionPageList(DistributionParams params, PageQuery pageQuery) {
        return memberDistributionRecordService.distributionPageList(params,pageQuery);
    }

    /**
     * 活动大屏数据
     */
    @GetMapping("/bigData/{activityId}")
    public R<Map> activityBigData(@NotNull(message = "活动ID不能为空") @PathVariable Long activityId) {
        return R.ok(activityService.activityBigData(activityId));
    }

    /**
     * 获取关于活动的微信订阅短链接
     */
    @GetMapping("/subscribeToUrl/{query}")
    public R<Map> getWxSubscribeToUrl(@PathVariable String query) {
        return R.ok(activityService.getWxSubscribeToUrl(query));
    }

    /**
     * 获取活动海报配置
     */
    @GetMapping("/posterConfig/{activityId}")
    public R<Map> getPosterConfig(@NotNull(message = "活动ID不能为空") @PathVariable Long activityId) {
        return R.ok(activityService.getPosterConfig(activityId));
    }

    /**
     * 修改海报配置
     */
    @PostMapping("/updatePosterConfig")
    public R<Void> updatePosterConfig(@Validated @RequestBody PosterConfigBO bo) {
        return toAjax(activityService.updatePosterConfig(bo));
    }

    /**
     * 管理后台获取模版海报图片
     */
    @PostMapping("/getTemplatePoster")
    public R<Map> getTemplatePosterImg(@RequestBody PosterBO bo) throws Exception {
        return R.ok(qrService.getTemplatePosterImg(bo));
    }

    /**
     * 获取客户推广短链接
     */
    @GetMapping("/memberPublicizeUrl/{memberId}/{activityId}")
    public R<Map> getMemberPublicizeUrl(@PathVariable("memberId") Long memberId,@PathVariable("activityId") Long activityId) {
        return R.ok(activityService.getMemberPublicizeUrl(memberId,activityId));
    }

    /**
     * 获取客户海报图片
     */
    @GetMapping("/memberPosterImg/{memberId}/{activityId}")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    public R<Map> getMemberPosterImg(@PathVariable("memberId") Long memberId,@PathVariable("activityId") Long activityId) throws Exception {
        return R.ok(qrService.getMemberPosterImg(memberId,activityId));
    }

    /**
     * 查询活动列表
     */
    @GetMapping("/orderList")
    public R<List<ActivityOrderListVO>> orderList(ActivityManageDetailParams params) {
        return R.ok(activityService.orderList(params));
    }

    /**
     * 查询活动清算列表（分页）
     */
    @GetMapping("/settlement/pageList")
    public TableDataInfo<ActivityVO> settlementPageList(ActivityDO dto, PageQuery pageQuery) {
        return activityService.settlementPageList(dto, pageQuery);
    }

    /**
     * 手动清算
     */
    @GetMapping("/manualSettlement/{activityId}")
    public R<Map> manualSettlement(@NotNull(message = "活动ID不能为空") @PathVariable Long activityId) {
        return R.ok(activityService.manualSettlement(activityId));
    }

    /**
     * 清算申请
     */
    @GetMapping("/settlement/apply/{activityId}")
    public R<Void> settlementApply(@NotNull(message = "活动ID不能为空") @PathVariable Long activityId) {
        return toAjax(activityService.settlementApply(activityId));
    }

    /**
     * 清算
     */
    @GetMapping("/settlement/{ids}")
    public R<Void> settlement(@PathVariable Long[] ids) {
        return toAjax(activityService.settlement(ids));
    }

    /**
     * 查询活动申请清算和已同意申请清算列表（分页）
     */
    @GetMapping("/settlement/apply/pageList")
    public TableDataInfo<ApplySettlementVO> applySettlementPageList(ActivityDO dto, PageQuery pageQuery) {
        return activityService.applySettlementPageList(null,dto, pageQuery);
    }

    /**
     * 活动拼团统计
     */
    @GetMapping(value = "/teamSta/{id}")
    public R<List<Map>> teamSta(@NotNull(message = "活动id不能为空") @PathVariable Long id) {
        return R.ok(activityService.teamSta(id));
    }
}
